package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CusterException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-06-06
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishService dishService;

    public void deleteById(Long id){
        //添加查询条件，通过id查询套餐数据
        LambdaQueryWrapper<Setmeal> qw =new LambdaQueryWrapper();
        qw.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(qw);
        if (count1>0){
            throw new CusterException("此数据关联菜品无法删除");
        }
        //添加查询条件，通过id查询菜品数据
        LambdaQueryWrapper<Dish> qw2 =new LambdaQueryWrapper();
        qw2.eq(Dish::getCategoryId,id);
        int count2 = dishService.count(qw2);
        if (count2>0){
            throw new CusterException("此数据关联菜品无法删除");
        }
        //调用删除方法
        super.removeById(id);
      //  this.removeById(id);
    }
}
